/* Task.h
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Header file for the Task class, representing a task in a to-do list.
*/

#ifndef __MZ_TASK_H__
#define __MZ_TASK_H__

#include "Selection.h"

#include <string>
#include <istream>
#include <ostream>

using std::string;
using std::istream;
using std::ostream;



class Task {
public:
	// Default constructor.
	Task();
	// Read a task from an input stream.
	Task(istream &in);
	// Copy constructor.
	Task(const Task &task);
	
	// Assignent.
	Task &operator=(const Task &task);
	
	// Compare by date.
	bool DateIsBefore(const Task &task) const;
	// Search the text of the task.
	// If the pattern is found, this function returns true and sets the given
	// selection to the range of character indices for the text.
	bool SearchText(const string &pattern, Selection &result, bool onlyMatchStart = false);
	// Collation.
	bool CollatesBefore(const Task &task);
	
	// Get or set the date.
	string Date() const;
	bool HasDate() const;
	void SetDate(int year, int month, int day);
	// Set the date based on a string. If the string is a single number, that number is
	// assumed to be a day in the current month (or the next month, if it is after the
	// current day). The same is done if no year is specified.
	// This will fail if the string contains any characters other than {0-9./'}.
	void SetDate(const string &date);
	void SetDateToToday();
	void ClearDate();

	// Get or set the text.
	const string &Text() const;
	void SetText(const string &text);
	
	// Get or set the depth.
	int Depth() const;
	void SetDepth(int depth);
	
	// Convenience functions for querying and setting certain flags.
	bool IsChecked() const { return GetFlag(CHECKED); }
	bool IsEmphasized() const { return GetFlag(EMPHASIZED); }
	bool IsDeleted() const { return GetFlag(DELETED); }
	
	bool IsRevealed() const { return GetFlag(REVEALED); }
	void Reveal(bool set = true) { SetFlag(REVEALED, set); }
	bool IsExpandable() const { return GetFlag(EXPANDABLE); }
	void SetExpandable(bool set = true) { SetFlag(EXPANDABLE, set); }
	bool IsExpanded() const { return GetFlag(EXPANDED); }
	void Expand(bool set = true) { SetFlag(EXPANDED, set); }

	// Get or set the given flag.
	bool GetFlag(unsigned short flag) const;
	void SetFlag(unsigned short flag, bool set);
	void ToggleFlag(unsigned short flag);

	// Convert the flags to a string.
	// This is used for clipboard input and output.
	string GetFlagString() const;
	void SetFlagString(const string &str);
	
	// Stream input and output functions.
	friend istream &operator>>(istream &in, Task &task);
	friend ostream &operator<<(ostream &out, const Task &task);
	
	
private:
	// Convert a character or string to lower case.
	// This is used for case-insensitive searching.
	static char ToLower(char c);
	static string ToLower(const string &s);
	
	
private:
	// The low byte of the flags is saved in the database.
	// The high byte (currently unused) will be for settings that are not saved,
	// such as filtering.
	unsigned short flags;
	unsigned char depth;
	unsigned char year;
	unsigned char month;
	unsigned char day;
	string text;
	
	
public:
	// Saved flags.
	static const unsigned short CHECKED = 0x01;
	static const unsigned short DELETED = 0x02;
	static const unsigned short REVEALED = 0x04;
	static const unsigned short EXPANDABLE = 0x08;
	static const unsigned short EXPANDED = 0x10;
	static const unsigned short EMPHASIZED = 0x20;
	
	// Date values.
	static const unsigned char IGNORE_DATE = 255;
	static const unsigned char GUESS_DATE = 254;
	
	// Temporary hack to allow reading files in the old format.
	static bool READ_LEGACY;
};



#endif

/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
